<?php
/*
 +----------------------------------------------------------------------
 + Title        : KDQuery 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/5/27 7:05
 + Last-time    : 2021/5/27 7:05+ chen2
 + Desc         : KDQuery
 +----------------------------------------------------------------------
*/

namespace kuaidi100;


use randy\units\ExtendHttp;

class KD
{
    protected $key;
    protected $customer;

    public function __construct()
    {
        $this->key = config('app.kd100')['key']; // 客户授权key
        $this->customer = config('app.kd100')['customer'];
    }

    public function query($logic_no, $company, $phone)
    {
        $param = [
            //			'com' => 'shunfeng',             //快递公司编码
            'com'      => $company,             //快递公司编码
            'num'      => $logic_no,     //快递单号
            'resultv2' => '1',            //开启行政区域解析
            'phone'    => $phone

        ];
        //请求参数
        $post_data["param"] = json_encode($param);
        $url = 'http://poll.kuaidi100.com/poll/query.do';    //实时查询请求地址
        $this->getSign($post_data);
        $res = ExtendHttp::post($url, $post_data);
        return $res;
    }

    // 签名
    public function getSign(&$post_data)
    {
        $post_data["customer"] = $this->customer;
        $sign = md5($post_data["param"] . $this->key . $post_data["customer"]);
        $post_data["sign"] = strtoupper($sign);
    }

    // 快递订阅通知
    public function poll($logic_no, $company, $reback)
    {
        $param = [
            //			'company' => 'shunfeng',             //快递公司编码
            'company'    => $company,             //快递公司编码
            'number'     => $logic_no,      //快递单号
            'key'        => $this->key,                    //客户授权key
            'parameters' => [
                'callbackurl' => $reback          //回调地址
            ]
        ];
        $post_data["param"] = json_encode($param);
        $url = 'http://poll.kuaidi100.com/poll';    //订阅请求地址
        $this->getSign($post_data);
        $res = ExtendHttp::post($url, $post_data);
        return $res;
    }

    /**
     * @param string $imageUrl
     *
     * @return bool|string
     * @author tao
     * @date   xxx
     */
    public function ocr($imageUrl = '')
    {
        $key = $this->key;                             // 客户授权key
        $param = [
            'image'      => null,                     // 图像数据，base64编码，要求base64编码后大小不超过4M,支持jpg/jpeg/png/bmp格式
            'enableTilt' => false,             // 是否兼容图像倾斜，true：是；false：否，默认不检测，即：false
            'include'    => null,                 // 需要检测识别的面单元素。取值范围：barcode,qrcode,receiver,sender,bulkpen
            'imageUrl'   => $imageUrl,                // 图片URL。image、imageUrl、pdfUrl三者必填其一，优先顺序：image>imageUrl>pdfUrl
            'pdfUrl'     => null,                  // PDF文件URL。image、imageUrl、pdfUrl三者必填其一，优先顺序：image>imageUrl>pdfUrl
        ];
        //请求参数
        $post_data = [];
        $post_data['key'] = $key;
        $post_data['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);

        $url = 'http://api.kuaidi100.com/elec/detocr';    // 快递面单OCR识别地址
        $res = ExtendHttp::post($url, $post_data);
        return $res;
    }
}